import React, { useState } from "react";
import { message } from 'antd';
import config from "../../config/config";
import * as chatDialogService from "./chatDialogService";
import * as settingService from "../../pages/home/services/settingService";
import defaultSetting from "../../pages/home/components/setting/DefaultSetting";

export function useChatDialogState() {

    const [historyRecords, setHistoryRecords] = useState([]);
    const [memoryDetails, setMemoryDetails] = useState([]);
    const [activeKey, setActiveKey] = useState("rag_setting");
    const [sourceId, setSourceId] = useState("");
    const [chatDialogModalVisible, setChatDialogModalVisible] = useState(false);
    const [selectedDialogKeys, setSelectedDialogKeys] = useState([]);
    const [selectedDialogRows, setSelectedDialogRows] = useState([]);
    const [ollamaModels, setOllamaModels] = useState([]);
    const [settingItemMap, setSettingItemMap] = useState({});

    const loadOllamaModels = () => {
        fetch(`${config.api.home.ollamaModelList}`).then(res => res.json()).then(res => setOllamaModels(res.data));
    }

    const load_history_records = (params) => {
        chatDialogService.getHistoryRecords(params).then(res => {
            if (res.code == 200) {
                setHistoryRecords(res.data);
            }
        });
    }

    const showMemoryDetail = (values) => {
        setSourceId(values.source_id);
        chatDialogService.getMemoryDetails(values).then(res => {
            if (res.code == 200) {
                setMemoryDetails(res.data);
            }
        });
    }

    const loadSettingItem = (params) => {
        settingService.getSettingList(params).then(res => {
            if (res.code == 200) {
                let settingObj = {};
                let settingItems = res.data;
                let defaultSettingItem = defaultSetting.default_setting[params["source"]];
                let defaultSettingKeys = Object.keys(defaultSettingItem);
                for (let i = 0; i < defaultSettingKeys.length; i++) {
                    let key = defaultSettingKeys[i];
                    let itemObj = settingItems.find(item => item.name == key);
                    if (itemObj && itemObj.content) {
                        settingObj[key] = itemObj.content;
                    } else {
                        settingObj[key] = defaultSettingItem[key];
                    }
                }
                setSettingItemMap(settingObj);
            }
        });
    }

    const onSaveDialog = (record) => {
        chatDialogService.updateHistoryRecord(record).then(res => {
            if (res.code == 200) {
                message.success("更新成功");
            }
        });
    }

    const deleteHistoryRecord = (values) => {
        let params = { ids: selectedDialogKeys.join(",") };
        chatDialogService.deleteHistoryRecord(params);
    }

    const downloadMemoryDetail = (values) => {
        values.source_id = values.id;
        chatDialogService.downloadMemoryDetails(values);
    }

    return {
        historyRecords, setHistoryRecords, load_history_records, showMemoryDetail, memoryDetails, setMemoryDetails, activeKey,
        setActiveKey, sourceId, setSourceId, chatDialogModalVisible, setChatDialogModalVisible, selectedDialogKeys,
        setSelectedDialogKeys, selectedDialogRows, setSelectedDialogRows, ollamaModels, loadOllamaModels, settingItemMap,
        loadSettingItem, onSaveDialog, downloadMemoryDetail, deleteHistoryRecord
    };
}
